package br.com.news.database.utils;

import java.io.Serializable;
import java.util.List;

import br.com.news.database.exception.DataBaseException;
import br.com.news.utils.beans.BaseBean;
import br.com.news.utils.database.exception.DataBaseServiceException;
import br.com.news.utils.database.interfaces.IServiceBaseDb;

public abstract class GenericService<T extends BaseBean, K extends BaseEntity, ID extends Serializable>
extends GenericServiceWithKey<T, K, ID, ID> implements IServiceBaseDb<T, ID> {

	/////////////////////////////////////////////////////////////////////////////////
	// PUBLIC METHODS (IServiceBase)
	/////////////////////////////////////////////////////////////////////////////////

	@Override
	public T findById(ID id) throws DataBaseServiceException {
		try {
			return parserEntity(getConcreteDao().findById(id));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException(e);
		}
	}

	@Override
	public List<T> findAll()  throws DataBaseServiceException {
		try {
			return parserEntity(getConcreteDao().findAll());
		} catch (DataBaseException e) {
			throw new DataBaseServiceException(e);
		}
	}

	@Override
	public ID insert(T bean) throws DataBaseServiceException {
			try {
				return getConcreteDao().save(parserBean(bean));
			} catch (DataBaseException e) {
				throw new DataBaseServiceException(e);
			}

	}

	@Override
	public void update(T bean) throws DataBaseServiceException {
		try {
			getConcreteDao().update(parserBean(bean));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException(e);
		}
	}

	@Override
	public void delete(T bean) throws DataBaseServiceException {
		try {
			getConcreteDao().delete(parserBean(bean));
		} catch (DataBaseException e) {
			throw new DataBaseServiceException(e);
		}
	}

}
